
/**
 * 两数之和
 *
 * @param nums 整数数组
 * @param target 目标值
 * @returns 包含目标值两个加数的索引的数组，若不存在则返回空数组
 */
function twoSum(nums: Array<number>, target: number): Array<number> {
    
    // let twoMap: {[index: number]: number} = {};
    let twoMap = {} as any;
    for(let i=0; i< nums.length; i++) {
        let num = nums[i];
        if(twoMap[target-num]) {
            return [i,twoMap[target-num]]
        }
        twoMap[num] = i;
    }
    return [];
};

console.log(twoSum([2,3,5,6], 9))



// 括号自闭函数
function isValide(str){
    let stack = [];
    let map = {
        '{': '}',
        '[': ']',
        '(': ')'
    }

    for(let i=0; i<str.length; i++) {
        let char = str[i];
        stack.push(char);
        if(stack.length < 2) {
            continue;
        }
        let theLastOne = stack[stack.length-1];
        let theLastTwo = stack[stack.length-2];
        if(char === map[theLastOne]) {
            stack.pop();
            stack.pop();
        }
    }
}
